/*******************************************************************************
* @Filename      : interrupt.c

* @Description   : interrupt.c

* @Version       : 1.0.0

* @Date          : 2023.09.25
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "app.h"

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Global variables ----------------------------------------------------------*/
uint16_t UART_RxLength=0,UART_TxLen=0,old_UART_RxLength=0;
uint32_t old_BSP_SYS_Tick;
uint8_t UART_RxBuffer[2148];
extern Device_State_Def my_device;
/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
extern uint8_t  touch_interrupt_flag;
/* Exported functions --------------------------------------------------------*/
/*******************************************************************************
 * @Name      : NMI_Handler
 * @Purpose   : NMI_Handler
 * @Param     : None
 * @Retval    : None
 *******************************************************************************/
void NMI_Handler(void)
{
}

/*******************************************************************************
 * @Name      : HardFault_Handler
 * @Purpose   : HardFault_Handler
 * @Param     : None
 * @Retval    : None
 *******************************************************************************/
void HardFault_Handler(void)
{
    while (1)
    {
    }
}

/*******************************************************************************
 * @Name      : SVC_Handler
 * @Purpose   : SVC_Handler
 * @Param     : None
 * @Retval    : None
 *******************************************************************************/
void SVC_Handler(void)
{
}

/*******************************************************************************
 * @Name      : PendSV
 * @Purpose   : PendSV
 * @Param     : None
 * @Retval    : None
 *******************************************************************************/
void PendSV_Handler(void)
{
}

/*******************************************************************************
 * @Name      : ADC1_IRQHandler
 * @Purpose   : ADC1_IRQHandler
 * @Param     : None
 * @Retval    : None
 *******************************************************************************/
void ADC1_IRQHandler(void)
{

}
////////////////////////////////////////////////////////////////////////
// TIM3 interrupt

void TIM3_IRQHandler(void)
{
    if (RESET != TIM_GetITStatus(TIM3, TIM_IT_Update))
    {
		//timer3_count++;
		BSP_SYS_US_Tick++;

        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
    }
}
/*******************************************************************************
 * @Name      : TIM14_IRQHandler
 * @Purpose   : TIM14_IRQHandler
 * @Param     : None
 * @Retval    : None
 *******************************************************************************/
void TIM14_IRQHandler(void)
{
    if (RESET != TIM_GetITStatus(TIM14, TIM_IT_Update))
    {
        TIM_ClearITPendingBit(TIM14, TIM_IT_Update);

        BSP_SYS_Tick++;
        BSP_SYS_2MS_Tick++;
        BSP_SYS_40MS_Tick++;
        BSP_SYS_1MS_Flag = 1;
		old_UART_RxLength = UART_RxLength;
	
        if (BSP_SYS_2MS_Tick >= 2)
        {
            BSP_SYS_2MS_Tick = 0;
            BSP_SYS_2MS_Flag = 1;
        }
        if (BSP_SYS_40MS_Tick >= 40)
        {
            BSP_SYS_40MS_Tick = 0;
            BSP_SYS_40MS_Flag = 1;
        }
    }
}

////////////////////////////////////
void UART2_IRQHandler(void)
{

    if (RESET != UART_GetITStatus(UART2, UART_IT_RXIEN))
    {
        UART_RxBuffer[UART_RxLength++] = UART_ReceiveData(UART2);
        old_BSP_SYS_Tick = BSP_SYS_Tick;
        UART_ClearITPendingBit(UART2, UART_IT_RXIEN);

    }

    if (RESET != (UART2->ISR & UART_ISR_RXIDLE))
    {
        UART2->ICR = UART_ICR_RXIDLE;
	}
}


/***********************************************************************************************************************
  * @brief  This function handles EXTI0_1 Handler
  * @note   none
  * @param  none
  * @retval none
  *********************************************************************************************************************/
void EXTI0_1_IRQHandler(void)
{
    #if !defined(USE_PD6_AS_MIC_PIN)
    /* MIC */
    if (RESET != EXTI_GetITStatus(MIC_PIN_EXTI_Line))
    {
        
        if(GPIO_ReadInputDataBit(MIC_PORT,MIC_PIN))
        {
            BSP_EXTI_Set_MIC_Signal(SIGNAL_EDGE_RISE);
        }
        else
        {
            BSP_EXTI_Set_MIC_Signal(SIGNAL_EDGE_FALL);
        }

        EXTI_ClearITPendingBit(MIC_PIN_EXTI_Line);
    }
    #endif
    
    /* CHG */
    if (RESET != EXTI_GetITStatus(CHG_DET_PIN_EXTI_Line))
    {
        if(GPIO_ReadInputDataBit(CHG_DET_PORT,CHG_DET_PIN))
        {
            BSP_EXTI_Set_CHG_DET_Signal(SIGNAL_EDGE_RISE);
        }
        else
        {
            BSP_EXTI_Set_CHG_DET_Signal(SIGNAL_EDGE_FALL);
        }

        EXTI_ClearITPendingBit(CHG_DET_PIN_EXTI_Line);
    }

	if (SET != EXTI_GetITStatus(TP_IRQ_PIN_EXTI_Line))
    {
        // tp wakeup mcu
        if(my_device.state == sleep)
        {
          RCC_LSICmd(ENABLE);
          my_device.state = wakeup;
        }
		else if(!touch_interrupt_flag)
		{
           touch_interrupt_flag = 1;
		}
        EXTI_ClearITPendingBit(TP_IRQ_PIN_EXTI_Line);
    }
}

/***********************************************************************************************************************
  * @brief  This function handles EXTI2_3 Handler
  * @note   none
  * @param  none
  * @retval none
  *********************************************************************************************************************/
void EXTI2_3_IRQHandler(void)
{
    /* K2 */
    if (RESET != EXTI_GetITStatus(EXTI_Line2))
    {

        
        EXTI_ClearITPendingBit(EXTI_Line2);
    }
}

/***********************************************************************************************************************
  * @brief  This function handles EXTI4_15 Handler
  * @note   none
  * @param  none
  * @retval none
  *********************************************************************************************************************/
void EXTI4_15_IRQHandler(void)
{
    #if defined(USE_PD6_AS_MIC_PIN)
    /* MIC */
    if (RESET != EXTI_GetITStatus(MIC_PIN_EXTI_Line))
    {
        
        if(GPIO_ReadInputDataBit(MIC_PORT,MIC_PIN))
        {
            BSP_EXTI_Set_MIC_Signal(SIGNAL_EDGE_RISE);
        }
        else
        {
            BSP_EXTI_Set_MIC_Signal(SIGNAL_EDGE_FALL);
        }

        EXTI_ClearITPendingBit(MIC_PIN_EXTI_Line);
    }
    #endif
}

/* Overrride functions -------------------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

/* End of File ---------------------------------------------------------------*/
